import {Request, Response, NextFunction} from 'express'
import {getSession} from '../util/SessionUtil';

// express要求的router中间件的格式 (req: Request, res: Response, next: NextFunction) => any
export const isValidUser = (req: Request, res: Response, next: NextFunction) => {
    console.log("执行isValidUser中间件函数...");
    let session = getSession(req);
    // userInfo对象是在登录后保存到sesson里的.
    if (session.userInfo && session.userInfo.mark === "noallowlogin") {
        res.setHeader("Content-Type", "text/html; charset=utf-8")
        res.write("您是被禁人士，被限制访问");
        res.end();
    } else {
        next();
    }
}

export const isVIPUser = (req: Request, res: Response, next: NextFunction) => {
    console.log("执行isVIPUser中间件函数...");
    let session = getSession(req);
    if (session.userInfo && session.userInfo.role === "vip") {
        console.log("vip用户上线...");
    }
    next();
}

export const isAdminUser = (req: Request, res: Response, next: NextFunction) => {
    console.log("执行isAdminUser中间件函数...");
    let session = getSession(req);
    if (session.userInfo && session.userInfo.username === "admin") {
        console.log("管理员用户上线...");
    }
    next();
}
